<template>
	<view class="login-content">
		<view class="title">
			<span>登录</span>
		</view>
		
		
		<view class="login-input-row">
			<view class="user" v-if="loginType">
				<u-input placeholder="输入手机号" v-model="mobile" clearable type="number" style="height:90rpx">
				</u-input>
			</view>
			<view class="user"  v-else>
				<u-input placeholder="输入账号" v-model="username" clearable type="text" style="height:90rpx">
				</u-input>
			</view>
			<view class="code" v-if="loginType">
				<u-input placeholder="请输入验证码" v-model="code" clearable style="height:90rpx;width: 70%;">
				</u-input>
				<u-toast ref="uToast"></u-toast>
					<u-verification-code :seconds="seconds" @end="end" @start="start" end-text="重新获取验证码" ref="uCode" 
					@change="codeChange"></u-verification-code>
					<span class="get-code" @tap="getCode">{{tips}}</span>
			</view>
			<view class="code" v-else>
				<u-input type="password" placeholder="请输入密码" v-model="password" clearable style="height:90rpx">
				</u-input>
			</view>
			<view class="forget-password-row" v-if="!loginType">
				<span class="forget-password" @tap="forgetHandle">忘记密码</span>
			</view>
			<view class="login-row" :style="loginType?'margin-top:60rpx':'margin-top:50rpx'">
				<u-button type="default" shape="circle" text="登录" class="login-btn" @tap="login"
					hover-class="none">登录</u-button>
			</view>
			<view class="select-type">
				<span @tap="changeType">{{changeTextShow()}}</span>
			</view>
		</view>
		<view class="other-way-row">
			<view class="select-way">
				<span @tap="wakeUpHandle('wx')">
					<image class="imgs" src="../../static/img/login/wx.png" alt=""></image> 
				</span>
				<span @tap="wakeUpHandle('qq')">
					<image class="imgs" src="../../static/img/login/qq.png" alt=""></image>
				</span>
				<span @tap="wakeUpHandle('wb')">
					<image class="imgs" src="../../static/img/login/wb.png" alt=""></image>
				</span>
			</view>
			<span class="prompt">验证即登录，未注册则自动创建新账号</span>
		</view>
		<!-- <myVerification v-if="codeFlag" @send="verification" class="verification" ref="verification"
			business="LOGIN" /> -->
	</view>
</template>

<script>
	import { API } from '../../config/myApi.js';
	import uuid from "@/utils/uuid.modified.js"; // uuid
	import myVerification from "@/components/verification/verification.vue"; //验证码模块
	import verifyCode from "@/components/verify-code/verify-code";
	import {
		whetherNavigate
	} from "@/utils/Foundation"; //登录跳转
	import storage from "@/utils/storage.js"; //缓存
	import {
		getUserInfo
	} from "@/api/members";
	export default {
		components: {
			myVerification,
			verifyCode,
			// wechatH5Login
		},
		data() {
			return {
				seconds: 60, //默认验证码等待时间
				codeFlag: true, //验证开关，用于是否展示验证码
				loginType: true,
				mobile: '',
				code: '',
				userType: '',
				password: '111111',
				username: "13011111111",
				isFirst: true,
				//角色
				role:'',
				
				tips: '',
				// refCode: null,
			}
		},
		
		onLoad(role){
			this.role = role.role;
		},
		methods: {
			// 验证码验证
			verification(val) {
				this.flage = val == this.$store.state.verificationKey ? true : false;
			},
			// 点击获取验证码
			
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				const pram = {
					mobile: this.mobile,
					verificationEnums: 'LOGIN'
				}
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					// uni.showLoading({
					// 	title: '正在获取验证码'
					// })
					console.log('kkk',this.mobile)
					if(!this.mobile) {
						this.$u.toast('手机号不能为空');
					} else {
					    API.getCodeByPhone(pram,pram.verificationEnums,pram.mobile).then(res=> {
					    	console.log('dddd',res)
					    })
						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							this.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						});
					}
					
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
				
			},
			start() {},
			/** 结束验证码后执行 */
			end() {
				// this.codeColor = this.lightColor;
				// this.codeFlag = true;
				// console.log(this.codeFlag);
			},
			login() {
				if(this.loginType) {
					this.smsLogin();
				}else {
					this.userLogin()
				}
				// this.loginHandle(this.isFirst);
			},
			userLogin() {
				const pwd = this.md5(this.password);
				let pram = {
					username: this.username,
					password: pwd
				}
				if(!this.username) {
					this.$u.toast('账号不能为空');
				} else if (!this.password) {
					this.$u.toast('密码不能为空');
				} else {
					// this.codeFlag = true;
					API.userLogin(pram).then(res=> {
						this.getUserInfoMethods(res);
						// console.log('5555',res)
						// if(res.code==200) {
							
						// 	const data = res.result
						// 	uni.setStorageSync('accessToken',data.accessToken);
						// 	// uni.setStorageSync('exists',data.exists);
						// 	uni.setStorageSync('username',pram.username);
						// 	// uni.setStorageSync('password',pram.password);
						// 	// if(data.exists==true) {
						// 	// 	API.getMember().then(data=> {
						// 	// 		console.log('eeed',data)
						// 	// 		if(data.code==200) {
						// 	// 			// uni.setStorageSync('userDetail',data.result)
						// 	// 			// const detail = uni.setStorageSync('userDetail')
						// 	// 			// console.log('detail++',detail)
						// 	// 		}
						// 	// 	})
						// 	// }
						// 	this.isFirst = res.result.exists;
						// 	this.loginHandle(this.isFirst);
						// 	API.getMember().then(user=>{
						// 		console.log(user);
						// 		storage.setUserInfo(user.result);
						// 		storage.setHasLogin(true);
						// 		// 登录成功
						// 		uni.showToast({
						// 			title: "登录成功!",
						// 			icon: "none",
						// 		});
						// 	})
						// 	// uni.navigateTo({
						// 	// 	url: '/pages/login/selectRole'
						// 	// })
						// 	// console.log('llll')
						// } else {
						// 	uni.showToast({
						// 		title: res.message,
						// 		duration: 2000,
						// 		icon: "none",
						// 	})
						// }
					})
				}
			},
			
			// 登录成功之后获取用户信息
			getUserInfoMethods(res) {
				
				if (res.code === 200) {
					storage.setAccessToken(res.result.accessToken);
					storage.setRefreshToken(res.result.refreshToken);
			
					/**
					 * 登录成功后获取个人信息
					 */
					getUserInfo().then((user) => {
						console.log(user);
						if (user.data.success) {
							/**
							 * 个人信息存储到缓存userInfo中
							 */
							storage.setUserInfo(user.data.result);
							storage.setHasLogin(true);
							// 登录成功
							uni.showToast({
								title: "登录成功!",
								icon: "none",
							});
							whetherNavigate();
			
						} else {
							uni.switchTab({
								url: "/pages/tabbar/home/index",
							});
						}
					});
				}
			},
			
			
			
			smsLogin() {
				const pram = {
					mobile: this.mobile,
					code: this.code
				}
				if(!this.mobile) {
					this.$u.toast('手机号不能为空');
				} else if (!this.code) {
					this.$u.toast('验证码不能为空');
				} else {
					API.smsLogin(pram).then(res=> {
						if(res.code==200) {
							console.log('5555',res)
							const data = res.result
							uni.setStorageSync('accessToken',data.accessToken);
							uni.setStorageSync('exists',data.exists);
							uni.setStorageSync('mobile',pram.mobile);
							uni.setStorageSync('code',pram.code);
						
							// API.getMember().then(data=> {
							// 	console.log('eeed',data)
							// 	if(data.code==200) {
							// 		// uni.setStorageSync('userDetail',data.result)
							// 		// const detail = uni.getStorageSync('userDetail')
							// 		// console.log('detail++',detail)
							// 	}
							// })
							this.isFirst = res.result.exists;
							console.log('888888',pram.mobile)
							this.loginHandle(this.isFirst);
						}else {
							uni.showToast({
								title: res.message,
								duration: 2000,
								icon: "none",
							})
						}
					})
				}
			},
			//点击登录
			loginHandle(isFirst) {
				console.log(isFirst);
				//判断是否首次登录
				if (isFirst) {
					uni.switchTab({
						url:'/pages/tabbar/home/index'
					})
				}else{
					uni.navigateTo({
						url: '/pages/login/selectRole'
					})
				}
			},
			//切换登录方式
			changeType() {
				// //置空
				// if (this.loginType) {
				// 	this.params.code = '';
				// } else {
				// 	this.params.password = '';
				// }
				//改变状态🐴
				this.loginType = !this.loginType;
			},
			//切换显示文本
			changeTextShow() {
				if (this.loginType) {
					return "帐号密码登录"
				} else {
					return "验证码登录"
				}
			},
			//唤醒其他登录
			wakeUpHandle(methods) {
				console.log(methods);
			},
			//忘记密码
			forgetHandle() {
				uni.navigateTo({
					url: "/pages/login/forgetPassword"
				})
			}
		},
		onload(res) {

		}
	}
</script>

<style lang="scss" scoped>
	.login-content {
		min-height: 100vh;
		background-color: #fff;
		overflow: hidden;

		.title {
			font-size: 46rpx;
			font-weight: bold;
			color: #333333;
			margin-top: 86rpx;
			text-align: center;
		}

		.login-input-row {
			padding: 0rpx 50rpx;
			margin-top: 190rpx;

			.user,
			.code {
				border-bottom: 1rpx solid #E3E3E3;
			}

			.code {
				margin-top: 50rpx;
				position: relative;

				.get-code {
					font-size: 26rpx;
					color: #F4333C;
					position: absolute;
					right: 0;
					top: 18rpx;
				}
			}

			.forget-password-row {
				margin-top: 26rpx;
				text-align: right;

				.forget-password {
					font-size: 26rpx;
					color: #F4333C;
				}
			}

			.login-row {
				.login-btn {
					background-color: #F4333C;
					color: #fff;
					height: 90rpx;
				}

				.u-hairline-border:after {
					border: none;
				}
			}

			.select-type {
				font-size: 26rpx;
				color: #F4333C;
				margin-top: 40rpx;
				text-align: center;
			}
		}

		.other-way-row {
			width: 100%;
			position: absolute;
			bottom: 80rpx;

			.select-way {
				display: flex;
				justify-content: center;

				>span {
					display: inline-block;
					width: 70rpx;
					height: 70rpx;
					border-radius: 35rpx;
					overflow: hidden;

					.imgs {
						width: 70rpx;
						height: 70rpx;
					}
				}

				>span:nth-of-type(2) {
					margin: 0rpx 60rpx;
				}
			}

			.prompt {
				font-size: 22rpx;
				color: #666666;
				display: block;
				text-align: center;
				margin-top: 61rpx;
			}
		}
	}
</style>
